package com.ruoyi.workbench.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.workbench.domain.WorkCollaboration;
import com.ruoyi.workbench.domain.WorkContent;
import com.ruoyi.workbench.domain.WorkMatterPracticable;
import com.ruoyi.workbench.mapper.WorkCollaborationMapper;
import com.ruoyi.workbench.mapper.WorkContentMapper;
import com.ruoyi.workbench.mapper.WorkMatterPracticableMapper;
import com.ruoyi.workbench.service.WorkMatterPracticableService;
import com.ruoyi.workbench.vo.SystemManageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 落实情况(WorkMatterPracticable)表服务实现类
 *
 * @author makejava
 * @since 2024-07-07 22:01:50
 */
@Service("workMatterPracticableService")
public class WorkMatterPracticableServiceImpl implements WorkMatterPracticableService {

    @Autowired
    private WorkMatterPracticableMapper workMatterPracticableMapper;
    @Autowired
    private WorkCollaborationMapper workCollaborationMapper;
    @Autowired
    private WorkContentMapper workContentMapper;
    @Override
    public AjaxResult saveWorkImplementation(SystemManageVo systemManageVo) {
        Integer matterId = systemManageVo.getMatterId();
        // 落实情况
        List<WorkMatterPracticable> workMatterPracticableList = systemManageVo.getWorkMatterPracticableList();
        workMatterPracticableMapper.deleteByMatterId(matterId);// 删除关联落实
        workMatterPracticableList.stream().forEach(workMatterPracticable -> {// 关联matterId
            workMatterPracticable.setWorkPrtPlanComltTime(systemManageVo.getWorkPrtPlanComltTime());
            workMatterPracticable.setMatterId(matterId);
            workMatterPracticable.setCreateTime(DateUtils.getNowDate());
//            workMatterPracticable.setWorkPrtPlanComltTime(systemManageVo.getPlannedCompletionTime());
        });
        if (workMatterPracticableList.size() > 0) {
            workMatterPracticableMapper.insertBatch(workMatterPracticableList);// 添加落实
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult saveContentAndColl(SystemManageVo systemManageVo) {
        Integer matterId = systemManageVo.getMatterId();
        //关键节点
        List<WorkContent> workContentList = systemManageVo.getWorkContentList();
        workContentMapper.deleteByMatterId(matterId);
        workContentList.stream().forEach(item->{
            item.setMatterId(matterId);
            item.setCreateTime(DateUtils.getNowDate());
        });

        workCollaborationMapper.deleteByMatterId(matterId);// 删除协同
        if (workContentList.size()>0) {
            workContentMapper.insertBatch(workContentList);


            // 添加协同需求
            for (WorkContent workContent : workContentList) {
                Long workContentId = workContent.getWorkContentId();

                List<WorkCollaboration> workCollaborationList = workContent.getWorkCollaborationList();
                workCollaborationList.stream().forEach(item -> {// 关联事项  节点
                    Long[] collaboratorsIdList = item.getCollaboratorsIdList();
                    StringBuilder sb = new StringBuilder();
                    for(Long userId:collaboratorsIdList){
                        sb.append(userId).append(",");
                    }

                    item.setCollaboratorsId(sb.toString());

                    item.setCollaborationRequestId(null);
                    item.setMatterId(matterId);
                    item.setEm1(workContentId.toString());
                    item.setCreateTime(DateUtils.getNowDate());
                });
                if (workCollaborationList.size() > 0) {
                    workCollaborationMapper.insertBatch(workCollaborationList);// 添加协同
                }
            }
        }


        return AjaxResult.success();
    }
}
